package com.huilian.iotbox.client.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置器
 * 公用一个交换机
 * 不同业务用不同的队列和key
 *
 * @Author mineChen
 * @Date 2021/3/23 21:15
 */
@Configuration
public class DelayMQConfig {

    /**
     * 延迟消息交换机
     */
    public static final String IOT_BOX_CLIENT_LAZY_EXCHANGE = "iotBox.client_lazy_exchange";


    /**
     * 延迟消息交换机
     */
    public static final String IOT_BOX_WAP_LAZY_EXCHANGE = "iotBox.wap_lazy_exchange";



    // 延迟队列-iotBox_client_lazy_queue
    public static final String IOT_BOX_CLIENT_LAZY_QUEUE = "iotBox_client_lazy_queue";
    // 延迟队列路由key
    public static final String BOX_CLIENT_LAZY_QUEUE_KEY = "iotBox.client_lazy_queue";

    // 售货机延迟队列
    public static final String IOT_BOX_CLIENT_vending_machine_LAZY_QUEUE = "iotBox_client_vending_machine_lazy_queue";

    // 售货机延迟队列路由key
    public static final String BOX_CLIENT_vending_machine_LAZY_QUEUE_KEY = "iotBox.client_vending_machine_lazy_queue";



    /**
     * 延迟发送查询基础参数给外部设备
     */
    public static final String IOT_BOX_WAP_SEND_MOTOR_DETECTION_LAZY_QUEUE = "iot_box_wap_send_motor_detection_lazy_queue";
    /**
     * 延迟发送查询基础参数给外部设备。延迟队列路由key
     */
    public static final String IOT_BOX_WAP_SEND_MOTOR_DETECTION_LAZY_QUEUE_KEY = "iot_box_wap_send_motor_detection_lazy_queue_key";

    @Bean
    public TopicExchange iotBoxClientLazyExchange() {
        Map<String, Object> pros = new HashMap<>();
        //设置交换机支持延迟消息推送
        pros.put("x-delayed-message", "topic");
        TopicExchange exchange = new TopicExchange(IOT_BOX_CLIENT_LAZY_EXCHANGE, true, false, pros);
        exchange.setDelayed(true);
        return exchange;
    }

    @Bean
    public TopicExchange iotBoxWapLazyExchange() {
        Map<String, Object> pros = new HashMap<>();
        //设置交换机支持延迟消息推送
        pros.put("x-delayed-message", "topic");
        TopicExchange exchange = new TopicExchange(IOT_BOX_WAP_LAZY_EXCHANGE, true, false, pros);
        exchange.setDelayed(true);
        return exchange;
    }


    @Bean
    public Queue iotBoxClientLazyQueue() {
        return new Queue(IOT_BOX_CLIENT_LAZY_QUEUE, true);
    }
    @Bean
    public Binding iotBoxClientLazyBinding() {
        return BindingBuilder.bind(iotBoxClientLazyQueue()).to(iotBoxClientLazyExchange()).with(BOX_CLIENT_LAZY_QUEUE_KEY);
    }

    @Bean
    public Queue iotBoxClientVendingMachineLazyQueue() {
        return new Queue(IOT_BOX_CLIENT_vending_machine_LAZY_QUEUE, true);
    }
    @Bean
    public Binding iotBoxClientVendingMachineLazyBinding() {
        return BindingBuilder.bind(iotBoxClientVendingMachineLazyQueue()).to(iotBoxClientLazyExchange()).with(BOX_CLIENT_vending_machine_LAZY_QUEUE_KEY);
    }


    @Bean
    public Queue iotBoxWapSendMotorDetectionLazyQueue() {
        return new Queue(IOT_BOX_WAP_SEND_MOTOR_DETECTION_LAZY_QUEUE, true);
    }
    @Bean
    public Binding iotBoxWapSendMotorDetectionLazyQueueValueLazyBinding() {
        return BindingBuilder.bind(iotBoxWapSendMotorDetectionLazyQueue()).to(iotBoxWapLazyExchange()).with(IOT_BOX_WAP_SEND_MOTOR_DETECTION_LAZY_QUEUE_KEY);
    }
}
